#include"string.h"

namespace zhao
{

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete _str;
			_str = tmp;
			_capacity = n;
		}
	}

	char& string::operator[](size_t index)
	{
		return _str[index];
	}

	void string::push_back(char c)
	{
		if (_size == _capacity)
		{
			reserve(_capacity = 0 ? 4 : 2 * _capacity);
		}
		_str[_size] = c;
		_size++;
	}


}

ostream& operator<<(ostream& out, const string& s)
{
	for (auto ch : s)
	{
		out << ch;
	}

	return out;
}


istream& operator>>(istream& in, string& s)
{
	s.clear();

	const int N = 256;
	char buff[N];
	int i = 0;

	char ch;
	//in >> ch;
	ch = in.get();
	while (ch != ' ' && ch != '\n')
	{
		buff[i++] = ch;
		if (i == N - 1)
		{
			buff[i] = '\0';
			s += buff;

			i = 0;
		}

		//in >> ch;
		ch = in.get();
	}



}
